package com.lanou.beautifulwardrobe.activity_zhuti.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.ProductDetailActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.IntroCategorie;
import com.lanou.beautifulwardrobe.entify.IntroProduct;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class IntroTSecListViewAdapter extends BaseAdapter {
    private List<IntroCategorie> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public void addData(List<IntroCategorie> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (datas != null && datas.size() > 0) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_introthreet_item_item, null);
                holder = new ViewHolder();
                holder.TSecImg = (NetworkImageView) convertView.findViewById(R.id.TSecImg);
                holder.TSecDecription = (TextView) convertView.findViewById(R.id.TSecDecription);
                holder.TSecPirce = (TextView) convertView.findViewById(R.id.TSecPirce);
                holder.TSecSmell = (TextView) convertView.findViewById(R.id.TSecSmell);
                holder.introthreeCardView= (CardView) convertView.findViewById(R.id.introthreeCardView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            switch (getItemViewType(position)) {
                case TYPE0:
                    List<IntroProduct> introProducts = datas.get(position).getProducts();
                    for (final IntroProduct introProduct : introProducts) {
                        holder.TSecDecription.setText(introProduct.getTaobao_title());
                        holder.TSecPirce.setText(introProduct.getMoney_symbol() + introProduct.getTaobao_price());
                        holder.TSecSmell.setText(introProduct.getMoney_symbol() + introProduct.getTaobao_selling_price());
                        holder.TSecPirce.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                        getBitMap(introProduct.getTaobao_pic_url(), holder.TSecImg);

                        holder.introthreeCardView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(parent.getContext(), ProductDetailActivity.class);
                                intent.putExtra("id",introProduct.getTaobao_num_iid());
                                parent.getContext().startActivity(intent);
                            }
                        });
                    }
                    break;
                case TYPE1:
                    final IntroProduct introProductq = datas.get(position).getProducts().get(0);
                    holder.TSecDecription.setText(introProductq.getTaobao_title());
                    holder.TSecPirce.setText(introProductq.getMoney_symbol() + introProductq.getTaobao_price());
                    holder.TSecSmell.setText(introProductq.getMoney_symbol() + introProductq.getTaobao_selling_price());
                    holder.TSecPirce.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    getBitMap(introProductq.getTaobao_pic_url(), holder.TSecImg);
                    holder.introthreeCardView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(parent.getContext(), ProductDetailActivity.class);
                            intent.putExtra("id", introProductq.getTaobao_num_iid()+"");
                            parent.getContext().startActivity(intent);
                        }
                    });
                    break;
            }


        }
        return convertView;
    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;

    @Override
    public int getItemViewType(int position) {
        if (datas != null && datas.size() > 0) {
            if (datas.get(position).getProducts().size() > 1) {
                return TYPE0;
            } else
                return TYPE1;
        }
        return 0;
    }

    class ViewHolder {
        private NetworkImageView TSecImg;
        private TextView TSecDecription, TSecPirce, TSecSmell;
        private CardView introthreeCardView;


    }

    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
